package io.renren.modules.gengyun.controller;


import io.renren.modules.gengyun.entity.PlanDate_Api;
import io.renren.modules.gengyun.entity.PlanGatherTimeEntity;
import io.renren.modules.gengyun.service.PlanGatherTimeService;
import io.renren.modules.gengyun.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("gengyun/gather")
@CrossOrigin
public class PlanGatherTimeController {

    private static SimpleDateFormat sf = null;
    @Autowired
    private PlanGatherTimeService gather_timeService;
    @PostMapping("/find")
    public Map<String,Object> find(Long id) throws IOException {
        System.out.println(id);
        PlanGatherTimeEntity gather_time=gather_timeService.getGather_time(id);
        System.out.println(gather_time.toString());
        Map<String,Object> resultMap = ResultUtils.getResponse(gather_time,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/add")
    public Map<String,Object> add(PlanGatherTimeEntity gather_time) throws IOException {
        System.out.println(gather_time);
        int  flag =gather_timeService.save(gather_time);
        System.out.println(gather_time.toString());
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/update")
    public Map<String,Object> update(PlanGatherTimeEntity gather_time) throws IOException {
        System.out.println(gather_time);
        int  flag =gather_timeService.update(gather_time);
        System.out.println(gather_time.toString());
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/delete")
    public Map<String,Object> delete(Long id) throws IOException {
        int  flag =gather_timeService.deleteById(id);
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/all")
    public Map<String,Object> all() throws IOException {

        List<Map<String,Object>>  allList=gather_timeService.allList();
        Map<String,Object> resultMap = ResultUtils.getResponse(allList,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/bydate")
    public Map<String,Object> bydate(Date date) throws IOException {
        sf = new SimpleDateFormat("yyyy-MM-dd");
        PlanDate_Api date1 = new PlanDate_Api(date);
        System.out.println(date1.getDate());
        System.out.println(sf.format(date1.getDate()));
        List<Map<String,Object>>  allList= gather_timeService.findByDateList(date1.getDate());
        Map<String,Object> resultMap = ResultUtils.getResponse(allList,"succeed",
                "internal error",false);
        return resultMap;
    }



}
